package com.leedsoft.news2.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import com.leedsoft.news2.R;

/**
 * Created by leeds on 2016/9/5 11:02.
 */
public class WebPageFragment extends Fragment {
    private String mUrl;
    private WebView mWebView;
    private ProgressBar mProgressBar;
    public static final String PAGE_URL = "PAGE_URL";

    public static WebPageFragment newInstance(String pageUrl) {
        Bundle args = new Bundle();
        args.putString(PAGE_URL, pageUrl);

        WebPageFragment fragment = new WebPageFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);

        mUrl = getArguments().getString(PAGE_URL);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.photo_web_page, container, false);

        mProgressBar = (ProgressBar) view.findViewById(R.id.photo_page_progressbar);

        mWebView = (WebView) view.findViewById(R.id.photo_page_webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;//使用webview打开webview中的链接
            }
        });
        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if(newProgress == 100) {
                    mProgressBar.setVisibility(View.INVISIBLE);
                } else {
                    mProgressBar.setVisibility(View.VISIBLE);
                    mProgressBar.setProgress(newProgress);
                }
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                System.out.println("<<<<<<<<<<<<<<<<<<<title: " + title);
            }
        });
        mWebView.loadUrl(mUrl);

        return view;
    }
}
